package com.dongbao.dialog;

import com.alibaba.fastjson.JSON;
import com.dongbao.data.Datacenter;
import com.dongbao.data.MessageData;
import com.dongbao.entity.MqMessage;
import com.dongbao.service.MyMqttClient;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.wm.ToolWindow;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Date;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

import static com.dongbao.common.MessageConstant.ALL_MESSAGE;

public class MessageWindow {

    private JPanel jcontent;
    private JTextArea messageContent;
    private JButton sendButton;
    private JButton seachService;
    private JCheckBox 共享本地服务CheckBox;
    private JList list1;
    private JTabbedPane tabbedPane1;
    private JTable codeErrorTable;

    public MessageWindow(Project project, ToolWindow toolWindow) {
        /* 发送按钮点击发送全局消息 */
        sendButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                MessageData messageData=new MessageData();
                messageData.setSendIp(Datacenter.clientIp);
                messageData.setSendTime(new Date());
                messageData.setContent(messageContent.getText());

                MqMessage mqMessage = MqMessage.getInstance();
                mqMessage.setData(messageData);
                mqMessage.setCode(ALL_MESSAGE);

                ExecutorService executor = Executors.newSingleThreadExecutor();
                Future<Integer> future = executor.submit(new Callable<Integer>() {
                    @Override
                    public Integer call() throws Exception {
                        Datacenter.myMqttClient.publishMessage(MyMqttClient.TOPIC_PREFIX+MyMqttClient.TOPIC_ALL, JSON.toJSONString(mqMessage),2);
                        return 3;
                    }
                });


            }
        });

        seachService.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

            }
        });
    }

    public JPanel getJcontent() {

        return jcontent;
    }

    private void createUIComponents() {
        // TODO: place custom component creation code here
    }
}
